﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DreamBuilder.Core.Data;
using CommonModule;
using ePlanet.ArchiveMange.Models;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Oracle;

namespace ePlanet.ArchiveMange.DataAccess
{
    public class ArchiveMenuDAL:DataHelper
    {
        public ArchiveMenuDAL()
        {
                if (!(this.DBHelper.database is OracleDatabase))
                {
                    throw new Exception("只支持Oracle数据库");
                }
        }

        public ArchiveMangeModal.ARCHIVEMENUDataTable GetAllArchiveMenu()
        {
            string sql = "select * from archivemenu";
            ArchiveMangeModal.ARCHIVEMENUDataTable data = new ArchiveMangeModal.ARCHIVEMENUDataTable();
            if (this.DBHelper.database is OracleDatabase)
            {
                DataSet ds = this.DBHelper.ExecuteDataSet(sql);
                if (ds != null && ds.Tables.Count > 0)
                {
                    ds.Tables[0].TableName = data.TableName;
                    data.Merge(ds.Tables[0]);
                }
                return data;
            }
            else
            {
                throw new Exception("只支持Oracle数据库");
            }
        }

        /// <summary>
        /// 保存强类型数据
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public bool SaveArchiveMenu(ArchiveMangeModal ds)
        {
            bool breturn = false;
            if (this.DBHelper.database is OracleDatabase)
            {
                this.InsertData(ds);
                breturn = true;
            }
            return breturn;
        }

        /// <summary>
        /// 根据主键查询数据
        /// </summary>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public ArchiveMangeModal.ARCHIVEMENUDataTable GetArchiveMenuById(string typeId)
        {
            string sql = "select * from archivemenu where typeId='"+typeId+"'";
            ArchiveMangeModal.ARCHIVEMENUDataTable data = new ArchiveMangeModal.ARCHIVEMENUDataTable();
            if (this.DBHelper.database is OracleDatabase)
            {
                DataSet ds = this.DBHelper.ExecuteDataSet(sql);
                if (ds != null && ds.Tables.Count > 0)
                {
                    data.Merge(ds.Tables[0]);
                }
                return data;
            }
            else
            {
                throw new Exception("只支持Oracle数据库");
            }
        }

        /// <summary>
        /// 根据用户的角色列表获取能看到的编目
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public ArchiveMangeModal.ARCHIVEMENUDataTable GetUserArhiveMenu(IList<string> list)
        {
            StringBuilder keys = new StringBuilder();
            foreach (string s in list)
            {
                keys.Append("'" + s + "',");
            }
            if (keys.Length > 0)
            {
                keys = keys.Remove(keys.Length - 1, 1);
            }
            else
            {
                keys.Append("''");
            }
            string sql = @"select am.* from archivemenu am  where am.TypeID in(
select archivemenuid from archivemenufunc where role_id in ("+keys+")) or am.ParentID in(select archivemenuid from archivemenufunc where role_id in ("+keys+"))";
            ArchiveMangeModal.ARCHIVEMENUDataTable data = new ArchiveMangeModal.ARCHIVEMENUDataTable();
            if (this.DBHelper.database is OracleDatabase)
            {
                DataSet ds = this.DBHelper.ExecuteDataSet(sql);
                if (ds != null && ds.Tables.Count > 0)
                {
                    ds.Tables[0].TableName = data.TableName;
                    data.Merge(ds.Tables[0]);
                }
                return data;
            }
            else
            {
                throw new Exception("只支持Oracle数据库");
            }
        }

        /// <summary>
        /// 根据主键查询档案的基本类型ID
        /// </summary>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public int GetArchiveBaseIDById(int Id)
        {
            string sql = "select * from archiveborrowtreemenu where typeId='" + Id + "'";
            ArchiveMangeModal.ARCHIVEMENUDataTable data = new ArchiveMangeModal.ARCHIVEMENUDataTable();
            if (this.DBHelper.database is OracleDatabase)
            {
                DataSet ds = this.DBHelper.ExecuteDataSet(sql);
                if (ds != null && ds.Tables.Count > 0)
                {
                    data.Merge(ds.Tables[0]);
                }
                ArchiveMangeModal.ARCHIVEMENURow row = (data.Rows[0] as ArchiveMangeModal.ARCHIVEMENURow);
                if (row.PARENTID > 0)
                {
                    return Convert.ToInt32(row.PARENTID);
                }
                else
                {
                    return Convert.ToInt32(row.TYPEID);
                }
            }
            else
            {
                throw new Exception("只支持Oracle数据库");
            }
        }

        /// <summary>
        /// 获取所有的基本档案编目
        /// </summary>
        /// <returns></returns>
        public ArchiveMangeModal.ARCHIVEMENUDataTable GetAllBaseMenu()
        {
            string sql = "select * from archivemenu t where t.parentid=0";
            ArchiveMangeModal.ARCHIVEMENUDataTable data = new ArchiveMangeModal.ARCHIVEMENUDataTable();
            if (this.DBHelper.database is OracleDatabase)
            {
                DataSet ds = this.DBHelper.ExecuteDataSet(sql);
                if (ds != null && ds.Tables.Count > 0)
                {
                    ds.Tables[0].TableName = data.TableName;
                    data.Merge(ds.Tables[0]);
                }
                return data;
            }
            else
            {
                throw new Exception("只支持Oracle数据库");
            }
        }


        public bool DeleteData(string key)
        {
            throw new Exception("尚未实现");
        }

        public bool IsSameSecret(List<Guid> dataIDList)
        {
            return true;
        }
    }
}
